<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------

namespace app\user\model;

use think\Exception;
use think\facade\Cache;
use think\Model;

/**
 * 角色模型
 * @package app\admin\model
 */
class Continent extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'area';

    function addAll($list,$parent_type,$updater){
        self::startTrans();
        $rep=[];
        try{
            foreach ($list as &$val){
                if($parent_type=='continent' && (empty($val['name'])
                    || empty($val['abbreviation'])
                    || (strlen($val['abbreviation'])!=3)
                    || empty($val['phone_code'])
                    || empty($val['parent_name'])
                    || empty($val['timezone'])
                    )
                ){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='excel file data is empty or format error!';
                    return $rep;
                }
                if($parent_type=='country' && (empty($val['name']) || empty($val['parent_name']))
                ){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='excel file data is empty!';
                    return $rep;
                }
                $val['parent_id']=self::where(['name'=>$val['parent_name'],'type'=>$parent_type])->value('id');
                if(empty($val['parent_id'])){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']=$parent_type.' name: '.$val['parent_name'].' is non-existent!';
                    return $rep;
                }
                $val['type']=($parent_type=='continent')?'country':'city';
                $count=self::where(['name'=>$val['name'],'type'=>$val['type']])->count();
                if($count){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']=$val['type'].' name:'.$val['name']. 'is repeat!';
                    return $rep;
                }
                $val['updater']=$updater;
                $val['update_time']=date('Y-m-d H:i:s');
                $val['create_time']=date('Y-m-d H:i:s');
                unset($val['parent_name']);
                $val['id']=self::insertGetId($val);
                if(!$val['id']){
                    self::rollback();
                    $rep['code']=-1;
                    $rep['msg']='Server Internal Error!';
                    return $rep;
                }

            }
            self::commit();
            //添加位置信息到redis
            if($parent_type=='country'){
                $handler=Cache::store('redis')->handler();
                foreach ($list as &$val){
                    if($val['latitude'] && $val['longitude']){
                        $handler->geoadd(config('redis.redis_key.city_location'),$val['longitude'],$val['latitude'],'city_'.$val['id']);
                    }
                }
            }
            $rep['code']=1;
            return $rep;
        }catch (\Exception $e){
            self::rollback();
            $rep['code']=-1;
            $rep['msg']='Server Internal Error!'.$e->getMessage();
            return $rep;
        }
    }
}
